package com.jijuxie.house.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jijuxie.common.core.R;
import com.jijuxie.house.domain.HouseAppointment;
import com.jijuxie.house.service.IHouseAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/house/appointment")
public class HouseAppointmentController {

    @Autowired
    private IHouseAppointmentService appointmentService;

    /**
     * 预约看房
     */
    @PostMapping
    public R<Void> add(@RequestBody HouseAppointment appointment) {
        appointment.setUserId(StpUtil.getLoginIdAsLong());
        appointmentService.addAppointment(appointment);
        return R.ok();
    }

    /**
     * 取消预约
     */
    @DeleteMapping("/{appointmentId}")
    public R<Void> cancel(@PathVariable Long appointmentId) {
        appointmentService.cancelAppointment(appointmentId);
        return R.ok();
    }

    /**
     * 获取预约列表
     */
    @GetMapping("/list")
    public R<Page<HouseAppointment>> list(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<HouseAppointment> page = appointmentService.selectAppointmentPage(
                new Page<>(pageNum, pageSize), StpUtil.getLoginIdAsLong());
        return R.ok(page);
    }

    /**
     * 经纪人确认预约
     */
    @PutMapping("/confirm/{appointmentId}")
    public R<Void> confirm(@PathVariable Long appointmentId) {
        appointmentService.confirmAppointment(appointmentId);
        return R.ok();
    }

    /**
     * 获取经纪人的预约列表
     */
    @GetMapping("/agent/list")
    public R<Page<HouseAppointment>> agentList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<HouseAppointment> page = appointmentService.selectAgentAppointmentPage(
                new Page<>(pageNum, pageSize), StpUtil.getLoginIdAsLong());
        return R.ok(page);
    }
}
